package logic

import (
	"context"
	"goods_srv/models/mysql"

	"goods_srv/goodssrv"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsRankLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsRankLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsRankLogic {
	return &GoodsRankLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsRankLogic) GoodsRank(in *goodssrv.GoodsRankRequest) (*goodssrv.GoodsRankResponse, error) {
	// todo: add your logic here and delete this line
	var goods []mysql.Goods

	mysql.DB.Debug().Order("sales_volume desc").Find(&goods)

	var goodsList []*goodssrv.GoodsList
	for _, v := range goods {
		goodsList = append(goodsList, &goodssrv.GoodsList{
			GoodsId:     int64(v.ID),
			Title:       v.Title,
			Cover:       v.Cover,
			Price:       float32(v.Price),
			SalesVolume: v.SalesVolume,
			Stock:       v.Stock,
			Status:      v.Status,
		})
	}
	return &goodssrv.GoodsRankResponse{
		List: goodsList,
	}, nil
}
